<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu-el.tld" prefix="menu-el" %>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tlds/authz.tld" prefix="authz" %>
<%@ taglib uri="/WEB-INF/tlds/sitemesh-decorator.tld" prefix="decorator"%>
<%@ taglib uri="/WEB-INF/tlds/ldap.tld" prefix="ldap"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="request"/>
<script type="text/javascript" src="${ctx}/dwr/interface/ConfigSaverDWR.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript">
function modifyRes() {
	var currentResId=checkValue($('applicationForm'),'resRadio');
	if(!currentResId || currentResId==0) {
		alert('<bean:message key="info.selectResource"/>');
	} else {
		popup('resource.do?method=edit&decorator=popup&confirm=true&currentResourceId='+currentResId, 'popupRes', 400, 410);
	}
}
function addRes() {
	var currentResId='';
	popup('resource.do?method=edit&decorator=popup&confirm=true&currentResourceId='+currentResId, 'popupRes', 400, 410);
}
function selectRes() {
	var currentResId=checkValue($('applicationForm'),'resRadio');
	$('applicationForm').action='application.do?method=listAttr&forward=resources&currentResourceId='+currentResId;
	$('applicationForm').submit();
}
function deleteRes() {
	var resVal=checkValue($('applicationForm'),'resRadio');
	if(!resVal || resVal.length==0) {
		alert('<bean:message key="info.selectResource"/>');
	} else if(confirm('<bean:message key="info.confirmDeletion"/>')) {
		$('methodRes').value='deleteRes';
		$('applicationForm').action='application.do?method=deleteRes&forward=resources&currentResourceId='+resVal;
		$('applicationForm').submit();
	}
}
function modifyAttr() {
	var resVal=checkValue($('applicationForm'),'resRadio');
	var attrVal=checkValue($('applicationForm'),'attrRadio');
	if(!resVal || resVal.length==0) {
			alert('<bean:message key="info.selectResource"/>');
	} else if(!attrVal || attrVal.length==0) {
			alert('<bean:message key="info.selectAttribute"/>');
	} else {
		popup('attribute.do?method=edit&decorator=popup&confirm=true&currentResourceId='+resVal+'&currentAttributeId='+attrVal, 'popupAttr', 400, 370);
	}
}
function addAttr() {
	var resVal=checkValue($('applicationForm'),'resRadio');
	if(!resVal || resVal.length==0) {
			alert('<bean:message key="info.selectResource"/>');
	} else {
		var attrVal='';
		popup('attribute.do?method=edit&decorator=popup&confirm=true&currentResourceId='+resVal+'&currentAttributeId='+attrVal, 'popupAttr', 400, 370);
	}
}
function deleteAttr() {
	var attrVal=checkValue($('applicationForm'),'attrRadio');
	var mandatoryAttr = '';
	if(attrVal && attrVal.length > 0)
		mandatoryAttr = $F("mandatoryAttr-"+attrVal);
	if(!attrVal || attrVal.length==0) {
		alert('<bean:message key="info.selectAttribute"/>');
	} else if(mandatoryAttr == 1) {
		alert('<bean:message key="info.canNotDeleteMandatoryAttr"/>');
	} else if(confirm('<bean:message key="info.confirmDeletion"/>')) {
		$('methodRes').value='deleteAttr';
		$('applicationForm').action='application.do?method=deleteAttr&forward=resources&currentAttributeId='+attrVal;
		$('applicationForm').submit();
	}
}

function saveConfig() {
	// AJAX call
	var id = $F("id");
	ConfigSaverDWR.saveApplicationConfig(message,id);
}					
function message(ret) {
	if(ret) {
		alert('<bean:message key="info.saveDone"/>');
		this.location = '${ctx}/application.do?method=list';
	} else {
			alert('<bean:message key="info.saveNotDone"/>');
	}
}
</script>
<div id="tabPanelWrapper">
	<html:form styleId="applicationForm" action="/application" >
	<div id="tabPanel">
		<ul>
			<li><a href="application.do?method=edit" onclick=""><bean:message key="application.generalTitle" /></a></li>
			<li><a id="currentTab" href="#" onclick=""><bean:message key="application.resourceTitle" /></a></li>
			<li><a href="variable.do?method=list" onclick=""><bean:message key="application.variableTitle" /></a></li>
		</ul>
	</div>
	<div id="tabContent">
			<h1><bean:write name="currentApplication" property="name" /></h1>
			<div id="subcontent">
				<input type="hidden" name="method" value="" id="methodRes" />
				<input type="hidden" name="forward" value="resource" />
				<html:hidden styleId="id" name="applicationForm" property="application.id" />
				<p><bean:message key="resourcePage.resourceList" /></p>
				<table class="list" id="resourceList">
					<thead>
						<tr>
							<th><bean:message key="resourcePage.selectResourceList" /></th>
							<th><bean:message key="resourcePage.nameResourceList" /></th>
							<th><bean:message key="resourcePage.typeResourceList" /></th>
						</tr>
					</thead>
					<tbody>
					<logic:present name="applicationForm" property="application.resources">
						<logic:iterate id="resource" name="applicationForm" property="application.resources" >							<tr>
							<tr>
								<td>
									<logic:present name="currentResource">
										<logic:equal name="currentResource" property="name" value="${resource.name}">
											<input type="radio" name="resRadio" id="resRadio" value="${resource.id}" checked="checked" onclick="selectRes();"/>
										</logic:equal>
										<logic:notEqual name="currentResource" property="name" value="${resource.name}">
											<input type="radio" name="resRadio" id="resRadio" value="${resource.id}" onclick="selectRes();" />
										</logic:notEqual>
									</logic:present>
									<logic:notPresent  name="currentResource">										
										<input type="radio" name="resRadio" id="resRadio" value="${resource.id}" onclick="selectRes();" />
									</logic:notPresent>
									
								</td>
								<td><bean:write name="resource" property="name"/></td>
								<td><bean:write name="resource" property="type"/></td>
							</tr>
						</logic:iterate>	
					</logic:present>					
					</tbody>
				</table>
				<p>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">				
					<input name="addResButton" type="submit" onclick="addRes();return false;" value="<bean:message key="resourcePage.addResource"/>" />&nbsp;
					<input name="modifyResButton" type="submit" onclick="modifyRes();return false;" value="<bean:message key="resourcePage.modifyResource"/>" />&nbsp;
					<input name="delResButton" type="submit" value="<bean:message key="resourcePage.deleteResource"/>" onclick="deleteRes();return false;" />
</authz:authorize>
<authz:authorize ifAllGranted="ROLE_USER">				
						<input name="displayResButton" type="submit" onclick="modifyRes();return false;" value="<bean:message key="resourcePage.displayResource"/>" />&nbsp;
</authz:authorize>

				</p>
					<div id="currentRes" name="currentRes">
					<p>
						<bean:message key="resourcePage.attributeTitle"/><i>[
						<logic:present name="currentResource" >
							<bean:write name="currentResource" property="name"/>
						</logic:present>]</i>
					</p>
					</div>
					<table class="list" id="attrList">
						<thead>
							<tr>
								<th><bean:message key="resourcePage.selectAttributeList" /></th>
								<th><bean:message key="resourcePage.nameAttributeList" /></th>
								<th><bean:message key="resourcePage.typeAttributeList" /></th>
								<th><bean:message key="resourcePage.mandatoryAttributeList" /></th>
								<th><bean:message key="resourcePage.ruleAttributeList" /></th>
							</tr>
						</thead>
						<tbody>
						<logic:present name="currentResource">
						<ldap:define-mandatory-attributes/>
						<logic:iterate id="attribute" name="currentResource" property="attributes" >
							<tr>
							<input type="hidden" name="mandatoryAttr" id="mandatoryAttr-${attribute.id}" value="${attribute.mandatory}"/>
							<logic:present name="currentAttribute">
							<logic:equal name="currentAttribute" property="name" value="${attribute.name}">
								<td><input type="radio" name="attrRadio" id="attrRadio"
									value="${attribute.id}" checked="checked"/></td>
							</logic:equal>
							<logic:notEqual name="currentAttribute" property="name" value="${attribute.name}">
								<td><input type="radio" name="attrRadio" id="attrRadio"
									value="${attribute.id}"/></td>
							</logic:notEqual>
							</logic:present>
							<logic:notPresent name="currentAttribute">
								<td><input type="radio" name="attrRadio" id="attrRadio"
									value="${attribute.id}"/></td>
							</logic:notPresent>
								<td><bean:write name="attribute" property="name"/></td>
								<html:hidden name="attribute" property="id"/>
								<td>
									<logic:equal name="attribute" property="inputOutput" value="0">
										<bean:message key="resourcePage.inputAttribute" />
									</logic:equal>
									<logic:equal name="attribute" property="inputOutput" value="1">
										<bean:message key="resourcePage.outputAttribute" />
									</logic:equal>
									<logic:equal name="attribute" property="inputOutput" value="2">
										<bean:message key="resourcePage.inputOutputAttribute" />
									</logic:equal>
								</td>
								<td>
									<logic:equal name="attribute" property="mandatory" value="1">
										<bean:message key="resourcePage.mandatoryStatus" />
									</logic:equal>
									<logic:equal name="attribute" property="mandatory" value="0">
										<bean:message key="resourcePage.notMandatoryStatus" />
									</logic:equal>
									<logic:equal name="attribute" property="mandatory" value="2">
										<bean:message key="resourcePage.unknownStatus" />
									</logic:equal>
								</td>
								<td>
									<logic:equal name="attribute" property="rule" value="" >
										<bean:message key="resourcePage.nonAppliedRuleAttribute" />
									</logic:equal>
									<logic:notEqual name="attribute" property="rule" value="" >
										<bean:message key="resourcePage.appliedRuleAttribute" />
									</logic:notEqual>
								</td>
							</tr>
						</logic:iterate>						
						</logic:present>
						</tbody>
					</table>
					<p>	
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">				
						<input name="addAttrButton" type="submit" onclick="addAttr();return false;" value="<bean:message key="resourcePage.addAttribute"/>" />&nbsp;
						<input name="modifyAttrButton" type="submit" onclick="modifyAttr();return false;" value="<bean:message key="resourcePage.modifyAttribute"/>" />&nbsp;
						<input name="deleteAttrButton" type="submit" value="<bean:message key="resourcePage.deleteAttribute"/>" onclick="deleteAttr();return false;" />
</authz:authorize>
<authz:authorize ifAllGranted="ROLE_USER">				
						<input name="displayAttrButton" type="submit" onclick="modifyAttr();return false;" value="<bean:message key="resourcePage.displayAttribute"/>" />&nbsp;
</authz:authorize>
					</p>
			</div>
			<p>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">								
				<input type="submit" value="<bean:message key='allPage.saveConfig' />" onclick="saveConfig();return false;"/>
</authz:authorize>
			</p>
		</div>
	</html:form>
</div>
